// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// V [author] rhyme_lph
// a [email]  rhymelph@gmail.com
// m [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/page/dashboard/purchase/views/purchases_bill_view.dart';
import 'package:finance_app/src/page/dashboard/purchase/views/purchases_expense_view.dart';
import 'package:finance_app/src/page/dashboard/purchase/views/purchases_po_view.dart';
import 'package:finance_app/src/page/dashboard/purchase/views/purchases_summary_view.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/button/assistant_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_svg_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/common_tab_bar.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/dashboard/purchase/purchases_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "purchasesPage")
class PurchasesPage extends StatefulWidget {
  final int index;

  const PurchasesPage({Key key, this.index}) : super(key: key);

  @override
  _PurchasesPageState createState() => _PurchasesPageState();
}

class _PurchasesPageState extends State<PurchasesPage>
    with
        PagePresenter<PurchasesPage, PurchasesProvider>,
        TickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    provider.currentIndex = widget.index ?? 0;
    _tabController = TabController(
        length: provider.tabList.length,
        vsync: this,
        initialIndex: widget.index ?? 0)
      ..addListener(() {
        provider.onChangeTab(_tabController);
      });
    provider.tabController = _tabController;
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.purchases, actions: [
        AssistantButton(
          entryPoint: 'Purchases',
        ),
      ]);

  @override
  Widget get buildWidget => listBuilder<bool>(
        selector: (_, __) => __.buttonBarIsUp,
        builder: (BuildContext context, bool value, Widget child) => Stack(
          children: [
            child,
            _buildBottomButton(value),
            // _buildTop(),
          ],
        ),
        child: _buildRecordList(),
      );

  Widget _buildBottomButton(bool isTop) => AnimatedPositioned(
      duration: Duration(milliseconds: 300),
      left: 0.0,
      right: 0.0,
      bottom: isTop
          ? MediaQuery.of(context).padding.bottom + getDp(40)
          : -getDp(60),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          NormalFloatSvgButton(
            onTap: provider.onNavAdd,
            asset: A.assetsImagesDashboardAdd,
          ),
        ],
      ));

  Widget _buildRecordList() => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            _buildTabBar(),
            Expanded(
                child: TabBarView(
                    controller: _tabController, children: _buildTabView())),
          ],
        ),
      );

  List<Widget> _buildTabView() => <Widget>[
        PurchasesSummaryView(),
        if (Config.hasPermission(PermissionUtils.purchaseOrder))
          PurchasesPoView(),
        if (Config.hasPermission(PermissionUtils.bill)) PurchasesBillView(),
        if (Config.hasPermission(PermissionUtils.expense))
          PurchasesExpenseView(),
      ];

  Widget _buildTabBar() => CommonTabBar(
        controller: _tabController,
        // isScrollable: true,
        tabs: provider.tabList
            .map((e) => MyTab(
                  text: '$e',
                ))
            .toList(),
      );
}
